<?php
/*
 * class SaeMysqlAdapter
*/
require_once ('mysql.adp.php');

class SaeMysqlAdapter extends mysqlAdapter {
	
	function adp_init($cfg) {
		$this->_conn = new SaeMysql();
		$this->_conn->setCharset('utf8');
		$this->_selectSchema = SAE_MYSQL_HOST_M;
	}
	
	function select($where = array() , $selected = "*", $ordered = NULL, $starts = NULL, $limits = NULL) {
		$args = array(
			'select',
			$where,
			$selected,
			$ordered,
			$starts,
			$limits
		);
		$__sql = call_user_func_array(array(
			$this,
			'_dumpSQL'
		) , $args);
		var_dump($__sql);
		$__res = $this->_conn->getData($__sql);
		if ($this->_conn->errno() != 0) {
			return NULL;
		}
		return $__res;
	}
	
	function insert($data) {
		$__keys = array_keys($data);
		$__vals = array_values($data);
		$__keys = join(',', $__keys);
		$__vals = F('warp', '\'', $__vals);
		$__vals = join(',', $__vals);
		$__sql = "INSERT INTO {$this->_table} ($__keys) VALUES ($__vals)";
		$this->_lastSQL;
		$this->_conn->runSql($__sql);
		if ($this->_conn->errno() == 0) {
			$this->_lastSucceed = FALSE;
			return FALSE;
		}
		else {
			$this->_lastSucceed = TRUE;
			$this->_lastId = $this->_conn->lastId();
			return TRUE;
		}
	}
	
	function delete($where) {
		foreach ($where as $k => $v) {
			$ws[] = $k . "='" . $v . "'";
		}
		$ws = join(" and ", $ws);
		$__sql = sprintf("delete from " . $this->_table . " where {$ws};");
		$__res = $this->_conn->runSql($__sql);
		if ($this->_conn->errno() == 0) {
			$this->_lastSucceed = FALSE;
			return FALSE;
		}
		else {
			$this->_lastSucceed = TRUE;
			$this->_lastId = $this->_conn->lastId();
			return TRUE;
		}
	}
	
	function update($where, $datas) {
		foreach ($where as $k => $v) {
			$ws[] = $k . "='" . $v . "'";
		}
		$ws = join(" and ", $ws);
		foreach ($datas as $k => $v) {
			$ds[] = $k . "='" . $v . "'";
		}
		$ds = join(",", $ds);
		$__sql = "update " . $this->_table . " set {$ds} where {$ws};";
		$__res = $this->_conn->runSql($__sql);
		if ($this->_conn->errno() == 0) {
			$this->_lastSucceed = FALSE;
			return FALSE;
		}
		else {
			$this->_lastSucceed = TRUE;
			$this->_lastId = $this->_conn->lastId();
			return TRUE;
		}
	}
	
	function save() {
	}
	
	function query($sql) {
	}
	
	function proc() {
	}
}
/**
 *  test
 *
 */
//$sae=new SaeMysqlAdapter();

//$where=array(),$selected="*",$ordered=null,$starts=null,$limits=null

//$sae->select('id=10 and var in (1,2,3) or ta>10',array('name','id'));
$str = 'id=10 and e=100 or j=ss and var in (1,2,3) or ta>200';
//|\(((\w+),?)+\)
$exp = "/(=|>|>=|<|<=)(\w+)/";
//preg_match_all($exp,$str,$match);
$str = preg_replace($exp, '${1}`${2}`', $str);
var_dump($str);
?>